package com.store.practice.dto;

import com.store.practice.model.BgStaff;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;

public class StaffUserDetails implements UserDetails {
    private BgStaff bgStaff;

    public StaffUserDetails(BgStaff bgStaff) {
        this.bgStaff = bgStaff;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        String authorities = bgStaff.getAuthority();
        if (authorities==null||authorities.length()==0){
            return null;
        }
        ArrayList<SimpleGrantedAuthority> result = new ArrayList<>();
        String[] authorityList = authorities.split(",");
        for (String authority:authorityList){
            SimpleGrantedAuthority authority1 = new SimpleGrantedAuthority(authority);
            result.add(authority1);
        }
        return result;
    }

    @Override
    public String getPassword() {
        return bgStaff.getPassword();
    }

    @Override
    public String getUsername() {
        return bgStaff.getAccount();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
